<template>
  <div class="app-container">
    <el-form :model="examForm" label-width="100px">
      <el-form-item label="考试名称">
        <el-input v-model="examForm.title"></el-input>
      </el-form-item>
      <el-form-item label="考试时间">
        <el-date-picker v-model="examForm.examTime" type="datetime" placeholder="选择时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="考试地点">
        <el-input v-model="examForm.location"></el-input>
      </el-form-item>
      <el-form-item label="监考老师">
        <el-input v-model="examForm.teacher"></el-input>
      </el-form-item>
      <el-form-item label="考试班级">
        <el-select v-model="examForm.classId" placeholder="请选择班级">
          <el-option v-for="item in classList" :key="item.classId" :label="item.name" :value="item.classId"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-divider>选择考试题目</el-divider>
    <el-table :data="questionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="title" label="题目内容"></el-table-column>
      <el-table-column prop="type" label="题型"></el-table-column>
      <el-table-column prop="score" label="分值"></el-table-column>
    </el-table>
    <el-button type="primary" @click="handlePublish">发布考试</el-button>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  data() {
    return {
      examForm: {
        title: '',
        examTime: '',
        location: '',   // 新增
        teacher: '',     // 新增
        classId: null // 新增
      },
      questionList: [],
      selectedQuestions: [],
      classList: [], // 新增
    }
  },
  created() {
    this.fetchQuestionList()
    this.fetchClassList(); // 新增
  },
  methods: {
    async fetchClassList() {
      // 假设后端有 /api/sysClass/getClassInfo?userId=xxx
      const userInfo = JSON.parse(localStorage.getItem('userInfo'));
      const res = await request.get('/api/sysClass/getClassInfo', { params: { userId: userInfo.id } });
      if (res.code === '200') {
        this.classList = res.data || [];
      }
    },
    async fetchQuestionList() {
      const res = await request.get('/api/questionBank/list')
      if (res.code === '200') {
        this.questionList = res.data.map(q => ({
          ...q,
          options: q.options ? JSON.parse(q.options) : []
        }))
      }
    },
    handleSelectionChange(val) {
      this.selectedQuestions = val
    },
    async handlePublish() {
      if (!this.examForm.title || !this.examForm.examTime || !this.examForm.location || !this.examForm.teacher || !this.        examForm.classId || this.selectedQuestions.length === 0) {
        this.$message.error('请填写完整考试信息并选择题目');
        return;
      }
      // 正确获取老师ID
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      const teacherId = (userInfo && userInfo.identity == 1 && userInfo.studentId !== 'admin') ? userInfo.id : null
      if (!teacherId) {
        this.$message.error('当前用户不是老师，无法发布考试')
        return
      }
      const res = await request.post('/api/exam/create', {
        ...this.examForm,
        teacherId,
        questionIds: this.selectedQuestions.map(q => q.id)
      })
      if (res.code === '200') {
        this.$message.success('试题发布成功')
        // 刷新题库列表
        this.fetchQuestionList()
        // 清空表单 
        this.examForm = { title: '', examTime: '', location: '', teacher: '' }
        this.selectedQuestions = []
        // 如果需要跳转页面，保留 this.$router.push('/publish/index')
      }
    }
  }
}

</script>